<template>
  <div>
    <!-- table -->
    <BreakSpace label="发票信息维护" :containerStyle="{ marginBottom: '10px' }" />
    <FormPanel ref="formPanel" :list="formList" :cols="3" :formType="'add'" labelWidth="110" :isSubmitBtn="false" @formChange="formChangeHandle" />
    <div style="padding-top: 3rem"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <multiuse-button type="primary" :click="saveDialog.bind()">添 加</multiuse-button>
      <multiuse-button @click="closeDialog">关 闭</multiuse-button>
    </div>
  </div>
</template>
<script>
import { BasicParametersAfterSales } from '@/api/dictionary/index';
import { getInvoiceInfo } from '@/api/claim/dealer/InvoiceManage';
import { invoice, invoice8 } from '@/utils/validate';
export default {
  props: ['invoiceList'],
  components: {},
  data() {
    return {
      dataList: [],
      dataList1: [],
      visible: false,
      visibleDrawer: false,
      collapse: false,
      infoId: null,
      flterValue: {}, //参数
      columns: this.creatHeader(),
      fetchParams: {}, //ipa 接口
      selectedRows: [], //选中经销商
      formList: this.createFormList(),
      // formList1: this.createFormList1(),
      saveData: null,
      activeName: '审核记录',
      saveDataFile: {},
      visibleList: false,
      map: {},
      invoiceMax: 0
    };
  },
  created() {
    this.chuqi();
  },
  methods: {
    async chuqi() {
      let res = await BasicParametersAfterSales();
      this.map.invoiceMax = res.data[2001];
      this.invoiceMax = this.map.invoiceMax;
      this.map.taxRate = res.data[2002];
      this.formList.find(x => x.fieldName == 'taxRate').initialValue = res.data[2002];
      // console.log(this.map);
      // const res = await getInvoiceInfo();
      // for (let i in res.data) {
      //   this.formList.map((y) => {
      //     if (i == y.fieldName) {
      //       y.initialValue = res.data[i];
      //     }
      //   });
      // }
    },
    formChangeHandle(val) {
      this.saveData = val;
      console.log(852);
      console.log(this.saveData);
    },
    //搜索获取数据
    changeHandle(val) {
      this.fetchParams = val;
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    deleteHandle(row) {
      this.$refs.table.$refs.pageTable.EXECUTE_DELETE([row]);
    },
    //保存
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      // this.saveData.invoiceMoney = parseFloat(this.saveData.invoiceAmout - this.saveData.taxAmount).toFixed(2);
      this.saveData.invoiceMax = this.invoiceMax;
      if (this.invoiceList.find(x => x.invoiceNo == this.saveData.invoiceNo)) {
        return this.$notify({
          title: '提示信息',
          message: '发票号已存在,请更换发票号!',
          type: 'error'
        });
      } else {
        console.log('进来了');
        this.$emit('save', this.saveData);
      }
    },
    //取消操作
    closeDialog() {
      this.$emit('close');
    },
    chooseOrderNo() {
      this.visible = true;
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '发票号',
          fieldName: 'invoiceNo',
          placeholder: '请输入发票号...',
          initialValue: '',
          rules: [
            { required: true, validator: invoice8, trigger: 'blur' },
            { required: true, message: '发票号码不能为空', trigger: 'blur' }
          ]
        },
        {
          type: 'INPUT',
          label: '开票金额',
          fieldName: 'invoiceAmout',
          placeholder: '请输入开票金额...',
          initialValue: '',
          rules: [
            { required: true, message: '请输入开票金额', trigger: 'blur' },
            {
              validator: (rule, value, callback) => {
                let reg = /^(\d+)(\.?)(\d{0,2})$/;
                if (!reg.test(Number(value))) {
                  return callback(new Error('开票金额,最多2位小数'));
                }
                if (parseFloat(parseFloat(this.map.invoiceMax).toFixed(2)) < parseFloat(parseFloat(value).toFixed(2))) {
                  return callback(new Error('开票金额,不可大于' + this.map.invoiceMax));
                }
                callback();
              },
              trigger: 'blur'
            }
          ],
          onInput: x => {
            let reg = /^(\d+)(\.?)(\d{0,2})$/;
            if (reg.test(Number(x))) {
              // this.formList.find((v) => v.fieldName == 'taxAmount').initialValue = parseFloat(Number(x) / (Number(this.formList.find((v) => v.fieldName == 'taxRate').initialValue)+1)).toFixed(2);
              let invoiceMoney = (x / (Number(this.formList.find(v => v.fieldName == 'taxRate').initialValue) + 1)).toFixed(2);
              this.formList.find(v => v.fieldName == 'invoiceMoney').initialValue = invoiceMoney;
              this.formList.find(v => v.fieldName == 'taxAmount').initialValue = (x - invoiceMoney).toFixed(2);
            }
          }
        },
        {
          type: 'INPUT',
          label: '税率',
          fieldName: 'taxRate',
          placeholder: '请输入税率...',
          initialValue: '',
          disabled: true,
          rules: [{ required: true, message: '请输入税率', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '税额',
          fieldName: 'taxAmount',
          placeholder: '请输入税额...',
          initialValue: '',
          rules: [{ required: true, message: '请输入税额', trigger: 'blur' }],
          readonly: true
        },
        {
          type: 'INPUT',
          label: '发票金额',
          fieldName: 'invoiceMoney',
          placeholder: '请输入发票金额...',
          initialValue: '',
          rules: [{ required: true, message: '请输入发票金额', trigger: 'blur' }],
          readonly: true
        }
      ];
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          render: props => {
            return (
              <div style="text-align:center">
                <multiuse-button size="mini" type="text" onClick={() => this.deleteHandle(props.row)}>
                  删除
                </multiuse-button>
              </div>
            );
          }
        },
        { title: '索赔单号', dataIndex: 'claimNo', sorter: true },
        { title: '索赔类型', dataIndex: 'cmsClaimTypeStr', sorter: true },
        { title: 'VIN', dataIndex: 'vin', sorter: true },
        { title: '旧件代码', dataIndex: 'partCode', sorter: true },
        { title: '旧件名称', dataIndex: 'partName', sorter: true },
        { title: '数量', dataIndex: 'partCount', sorter: true },
        { title: '销毁数量', dataIndex: 'pestructionCount', sorter: true, editable: true, editType: 'input', defaultEditable: true },
        { title: '销毁说明', dataIndex: 'partRemarks', sorter: true, editable: true, editType: 'input', defaultEditable: true }
      ];
    }
  }
};
</script>

<style lang="less" scoped></style>
